package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;

@WebServlet(urlPatterns = "/request")
public class RequestServletDemo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // localhost/test?name=zs&password=qwe
        System.out.println(req);
        System.out.println("项目访问路径:" + req.getContextPath());
        StringBuffer requestURL = req.getRequestURL();
        System.out.println("请求的URL:" + requestURL);
        System.out.println("请求的URI:" + req.getRequestURI());
//        req.getParameter("name")
        // 获取提交的所有参数名称
//        Enumeration<String> parameterNames = req.getParameterNames();
//        while (parameterNames.hasMoreElements()) {
//            String name = parameterNames.nextElement();
//            // 获取某个提交的参数的值 username password
//            String value = req.getParameter(name);
//            System.out.println(name + "==>" + value);
//        }
//        System.out.println("请求方式" + req.getMethod());
//        Enumeration<String> headerNames = req.getHeaderNames();
//        Iterator<String> iterator = headerNames.asIterator();
//        while (iterator.hasNext()) {
//            String name = iterator.next();
//            // 获取指定的请求头
//            String value = req.getHeader(name);
//            System.out.println(name + " == " + value);
        // get方式的请求参数 ? 后面拼接的内容
        System.out.println(req.getQueryString());
        String queryString = req.getQueryString(); // username=qwwq&password=wqwqw
        String[] split = queryString.split("&"); // username=qwwq  password=wqwqw
        for (String s : split) {
            String[] keyAndValue = s.split("="); // username qwwq
            System.out.println("key = " + keyAndValue[0]);
            System.out.println("value = " + keyAndValue[1]);
        }
        // getHeader 获取指定请求头的信息
        System.out.println("客户端信息：" + req.getHeader("user-agent"));
        // 本次请求中所有请求头的key的信息
        Enumeration<String> headerNames = req.getHeaderNames();
//        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("method:" + request.getMethod());
        System.out.println("query" + request.getQueryString());
        // 返回字符流输入流
//        BufferedReader reader = request.getReader();
//        String line;
//        while ((line = reader.readLine()) != null) {
//            System.out.println(line); // username=dasdasd&password=dsadsadas
//        }
        // 获取请求体内容的字节输入流
        ServletInputStream inputStream = request.getInputStream();
        System.out.println(inputStream.available());
        FileOutputStream out = new FileOutputStream("E:/home/test.png");
        inputStream.transferTo(out);
        out.flush();
        out.close();
        inputStream.close();
    }
}
